﻿using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Authorization;
using GoodDotNet.WebAdmin.Controllers;

using GoodDotNet.Service;
using GoodDotNet.WebAdmin.HttpContextUser;
using Microsoft.Extensions.Options;
using GoodDotNet.Core.Data;
using GoodDotNet.Core.Model;

namespace GoodDotNet.Webadmin.Controllers
{
    [ApiController]
	[Route("api/[controller]")]
	public class AdminRoleController : BaseController
	{
        public AdminRoleController(IUser user, IHttpContextAccessor accessor, IOptions<List<ConnectionString>> conns, ILogger<BaseController> logger, IOptions<Appsettings> appsettings) : base(user, accessor, conns, logger, appsettings)
        {
        }

        [HttpPost]
		[Route("Add")]
		[Authorize]
		public Result Add([FromBody] ParInsertAdminRoleModel pars)
		{
		   
		        using (var uniOfWork = new UnitOfWork(_conStr))
		        {
		           AdminRoleService service = new AdminRoleService(uniOfWork);
		                service.Add(pars,_user.Id,pars.site);
		        }
		        return Sucess("sucess");
		 
		
		}
		/// <summary>
        /// 站点角色
        /// </summary>
        /// <param name="pars">站点id</param>
        /// <returns></returns>
		[HttpPost]
        [Route("list")]
        [Authorize]
        public Result List()
        {
             using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminRoleService service = new AdminRoleService(uniOfWork);
                var roles=service.GetList();
                return   Sucess(roles);
            }

        }
       


        [HttpPost]
        [Route("Update")]
        [Authorize]
        public Result Update([FromBody] ParUpdateAdminRoleModel pars)
        {

            using (var uniOfWork = new UnitOfWork(_conStr))
            {
                AdminRoleService service = new AdminRoleService(uniOfWork);
                service.Update(pars.Id, pars.RoleName, pars.Description, _user.Id);
            }
            return Sucess("sucess");


        }
				
		
	
				
		[HttpPost]
		[Route("Delete")]
		[Authorize]
		public Result Delete([FromBody] ParDeleteAdminRoleModel pars)
		{
		  
		        using (var uniOfWork = new UnitOfWork(_conStr))
		        {
		           AdminRoleService service = new AdminRoleService(uniOfWork);
		                service.Delete(pars);
		        }
		            return Sucess("sucess");
		   
		
		}
		}
}
